﻿using System.Diagnostics;
using System.Windows;
using Microsoft.Win32;

namespace MyEditor;

class Registry_Add
{
    
    
    // Registry.ClassesRoot 映射到 HKEY_CLASSES_ROOT，通常需要管理员权限
    // Registry.CurrentUser 映射到 HKEY_CURRENT_USER，仅需要当前用户的权限, 权限要求较低
    
    // 注册表 ROOT 用户:
    // Computer\HKEY_CLASSES_ROOT\Directory\shell
    // Computer\HKEY_CLASSES_ROOT\Directory\Background\shell
    // Computer\HKEY_CLASSES_ROOT\Directory\shell
    // 
    // 注册表 当前用户:
    // Computer\HKEY_CURRENT_USER\Software\Classes\Directory\shell
    // Computer\HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell
    // Computer\HKEY_CURRENT_USER\Software\Classes\*\shell
    // Computer\HKEY_CURRENT_USER\Software\Classes\.txt\shell 特定文件类型右键菜单（如文本文件）  

    
    
    


    public static void Register_Context_Menu()
    {
        try
        {
            string exePath = Process.GetCurrentProcess().MainModule.FileName;
            string menuText = $"Open Folder with {MyConfig.AppName}";

            // 文件夹右键
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(
                       $@"Software\Classes\Directory\shell\{MyConfig.AppName}"))
            {
                key.SetValue("", menuText);
                using (RegistryKey commandKey = key.CreateSubKey("command"))
                {
                    commandKey.SetValue("", $"\"{exePath}\" \"%1\"");
                }
            }

            // 文件夹背景右键
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(
                       $@"Software\Classes\Directory\Background\shell\{MyConfig.AppName}"))
            {
                key.SetValue("", menuText);
                using (RegistryKey commandKey = key.CreateSubKey("command"))
                {
                    commandKey.SetValue("", $"\"{exePath}\" \"%V\"");
                }
            }

            MessageBox.Show("成功 : Context menu registered successfully!");
        }
        catch (Exception ex)
        {
            MessageBox.Show($"错误 : Failed to register context menu. Error: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }



    
    
    
    
    
    
    
    
    
    
    
    
    
}